package weaver.interfaces.cese.duss.Test;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.interfaces.cese.duss.ceseutil.HttpRequestUtil;
import weaver.interfaces.cese.duss.ceseutil.OcrUitl;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-04-30
 * @Description: ${description}
 * @ModifyLog:
 **/

public class YZTest {
    //发票验真接口
    public static JSONObject getInvoiceValidate(String urlString, String API_KEY, JSONObject params) {
        Map<String, String> headers = new HashMap<>();
        JSONObject jsonObject = new JSONObject();
        headers.put( "MediaType", "application/octet-stream" );
        headers.put( "Content-Type", "application/json" );
        headers.put( "X-API-Key", API_KEY );
        jsonObject.put( "Data", params );
        String result = OcrUitl.httpPostRaw( urlString, jsonObject.toJSONString(), headers, "UTF-8" );
        JSONObject resultJson = JSONObject.parseObject( result );//发票识别的具体信息
        return resultJson;
    }

    //发票认证
    public static Map<String, Object> invoicerecertify(String invoiceType, String invoiceCode, String invoiceNum, String invoiceDate, String totalAmount, String checkCode, String sellerRegisterNum) {
        String YZ_API_KEY = "2d71665b-9f03-4eae-b104-3a985709a8f0";
        String yz_url = "https://api.inspuronline.com/business/cn-vat-receipt/v2/invoice-check-info";
        System.out.println( "----------发票验真----------" );
        String msg = "";
        boolean flag;
        Map<String, Object> dataMap = new HashMap<>();
        JSONObject params = new JSONObject();
        params.put( "InvoiceType", invoiceType );//发票类型
        params.put( "InvoiceCode", invoiceCode );//发票代码
        params.put( "InvoiceNumber", invoiceNum );//发票号码
        params.put( "DateOfInvoice", invoiceDate );//开票日期
        params.put( "Amount", totalAmount );//未税金额
        String subCheckCode = checkCode.length() >= 6 ? checkCode.substring( checkCode.length() - 6 ) : checkCode;
        params.put( "CheckCode", subCheckCode );//校验码后六位
        params.put( "TaxpayerIDNo", sellerRegisterNum );//税号
        System.out.println( "params=" + params );
        JSONObject returnJson = getInvoiceValidate( yz_url, YZ_API_KEY, params );
        if(returnJson.isEmpty()){msg="接口异常";}
        System.out.println( "returnJson=" + returnJson );
        JSONObject ReturnInfo = JSONObject.parseObject( returnJson.getString( "ReturnInfo" ) );
        String Code = ReturnInfo.getString( "Code" );
        String Message = ReturnInfo.getString( "Message" );
        if ("0".equals( Code )) {//验真通过

            flag = true;
            msg = Message;
        } else {
            flag = false;
            msg = Message;
        }
        dataMap.put( "flag", flag );
        dataMap.put( "msg", msg );
        return dataMap;
    }
    //查询底账库是否存在发票
    public static boolean getInvoiceStoreQuery(String invoicenum,String invoicecode,String buyertaxno){
        boolean flag = true;
        String url = "http://172.16.91.32:8045/cwbase/service/jzstandiface/InterfaceTransformAPI.ashx/InvoiceStoreQuery";
        JSONObject dataJson = new JSONObject(  );
        dataJson.put("INVOICENUM",invoicenum);
        dataJson.put("INVOICECODE",invoicecode);
        dataJson.put("BUYERTAXNO",buyertaxno);
        JSONObject reustl = HttpRequestUtil.sendPostHttpRequest(url,dataJson.toJSONString());
        String code = reustl.getString( "code" );
        String msg = reustl.getString( "msg" );
        System.out.println("reustl="+reustl.toJSONString());
        if(!"1".equals( code ) || !"".equals( msg )){
            flag = false;
        }
        return flag;
    }
    public static void main(String[] args) {
        //增值税普通发票-04
        //增值税专用发票-01
        //增值税电子普通发票-10
        //机动车销售统一发票-03
        //增值税普通发票（卷式）-11
        String invoiceType = "01";
        String invoiceCode = "1100211130";
        String invoiceNum = "03212163";
        String invoiceDate = "2021-04-26";
        String totalAmount = "2183.62";
        String checkCode = "";
        String sellerRegisterNum = "91320200134757148J";
        Map<String, Object> resultMap = invoicerecertify( invoiceType, invoiceCode, invoiceNum, invoiceDate, totalAmount, checkCode, sellerRegisterNum );
        //boolean dzkyz = getInvoiceStoreQuery(invoiceNum,invoiceCode,sellerRegisterNum);
       // System.out.println( "验真接口返回----flag=" + resultMap.get( "flag" ) + ", msg=" + resultMap.get( "msg" ) );

        System.out.println( "底账库接口返回----flag=" + resultMap );
    }
}